package com.automapper.criterion;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.automapper.exception.AutoMapperException;

public class Disjunction implements Criterion {
	
	private Criterion[] criterions;
	
	public Disjunction(Criterion[] criterions) {
		AutoMapperException.ArrayIsBlankRuntimeException(criterions, "Restrictions.or方法的参数不能为null");
		for (Criterion criterion : criterions) {
			AutoMapperException.NullRuntimeException(criterion, "Restrictions.or方法的参数不能为null");
		}
		this.criterions = criterions;
	}

	@Override
	public String toSqlString() {
		List<String> list = new ArrayList<>();
		for (Criterion criterion : criterions) {
			list.add(criterion.toSqlString());
		}
		
		StringBuilder sb = new StringBuilder();
		sb.append(" (");
		sb.append(StringUtils.join(list, " or "));
		sb.append(") ");
		return sb.toString();
	}

	@Override
	public String getParameterAliases() {
		return null;
	}

	@Override
	public Object getValue() {
		return null;
	}

	@Override
	public Map<String, Object> getValues() {
		Map<String, Object> map = new HashMap<>();
		for (int i = 0; i < criterions.length; i++) {
			Criterion criterion = criterions[i];
			map.put(criterion.getParameterAliases(), criterion.getValue());
		}
		return map;
	}

	@Override
	public List<String> getAliase() {
		List<String> list = new ArrayList<>();
		for (Criterion criterion : criterions) {
			list.addAll(criterion.getAliase());
		}
		return list;
	}

}
